<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, minimum-scale=1, user-scalable=no,maximum-scale=1">
    <title>开发中</title>
    <style>
        textarea,input,button {
            width: 300px;height: 40px;font-size: 20px;
            margin: 5px;
        }
    </style>
</head>
<body>
<textarea type="text" id="message" rows="10">249</textarea><br>
<input type="text" id="speed" value="1" /><br>
<button onclick="Login_AuthSuper()">密码登录</button><br>
<button onclick="getAlarmItem()">获取预警设置信息</button><br>
<button onclick="updateAlarmItem()">更新预警设置信息</button><br>

<button onclick="VTaskList()">获取任务模式</button><br>
<button onclick="VTaskOptions2()">获取任务子模式</button><br>
<button onclick="VTaskOptions3()">获取下拉选项3(母艇, 假艇)</button><br>
<button onclick="VTaskOptions()">获取任务预警项</button><br>
<button onclick="TaskSave()">开始任务 (新增 | 编辑)</button><br>

<button onclick="TaskList()">获取任务列表</button><br>
<button onclick="TaskDetail()">获取任务详情</button><br>
<button onclick="TaskFinish()">任务结束</button><br>
<button onclick="ReadTaskData()">读取任务数据</button><br>
<button onclick="SetTimerInterval()">倍速播放</button><br>
<button onclick="ReadTaskDataPause()">暂停</button><br>
<button onclick="ReadTaskDataPlay()">继续</button><br>
<button onclick="ClearTimerInterval()">停止</button><br>
<button onclick="JumpToRecord()">跳转录像进度</button><br>
<button onclick="GetCurrentTaskId()">获取正在进行中的任务id</button><br>
<button onclick="UpdateSimulationSettings()">仿真参数保存</button><br>
<button onclick="UpdateForecastSettings()">预测参数更新</button><br>
<button onclick="GetSimulationSettings()">仿真参数查看</button><br>
<button onclick="ToPush()">topushdata</button><br>

<!-- <button onclick="test()">test</button><br>-->
<script>

    let socket;
    let reconnectAttempts = 0; // 当前重连次数
    let reconnecting = false; // 标志是否正在尝试重连
    let loginStatus = false
    let msgEl = document.getElementById("message")
    let speedEl = document.getElementById("speed")

    function connectWebSocket() {
        reconnecting = true
        // socket = new WebSocket("ws://192.168.220.53:11234");
        socket = new WebSocket("ws://frp.psn.asia:11235");

        socket.onopen = function (res) {
            loginStatus = true
            reconnecting = false; // 重置重连状态
            console.log("连接成功");
            reconnectAttempts = 0; // 重置重连计数
        }

        socket.onclose = function (e) {
            loginStatus = false
            reconnecting = false
            console.log("WebSocket closed");

        }

        socket.onerror = function (err) {
            console.log("WebSocket error")
        }

        socket.onmessage = function(e) {
//            console.log(typeof e.data,e.data);
            // console.log("响应原始数据",e.data);
            let res = JSON.parse(e.data)
            // console.log("响应JSON数据:", res);
            if(res.code === 0) {
                // console.log("success:", res.data);
                if(['SetTimerInterval','ReadTaskData','ReadTaskDataPause','ReadTaskDataPlay'].indexOf(res.ts) !== -1) {
                    if(res.ts === 'ReadTaskData') {
                        let data = JSON.parse(res.data)
                        console.log(res.ts,data)
                    }else {
                        console.log(res.ts,res)
                    }

                    // if(data.list) {
                    //     console.log("data.list: ",JSON.parse(data.list))
                    // }
                }
            }else {
                console.log("error:",res.msg);
            }
        };
    }
    // 启动 WebSocket 连接
    connectWebSocket();

    function test() {
        // console.log("msgEl.value",msgEl.value)
        socket.send(msgEl.value)
    }
    function getTs() {
        let now = new Date()
        return String(now.getTime())
    }
    function getAlarmItem() {
        // let param = '{"route":"TaskCurrent","ts":"ababa123123","params":{"password":"123123"}}'
        let param = '{"route":"GetAlarmItem","ts":"GetAlarmItem","params":{"itemid":"1"}}'
        console.log("发送参数: ", param)
        socket.send(param)
    }
    function updateAlarmItem() {
        let data = [
            {"id":"1","value":"300","min_value":"5","max_value":"660"},
            {"id":"2","value":"250","min_value":"5","max_value":"700"},
            {"id":"3","value":"200","min_value":"5","max_value":"600"},
            {"id":"4","value":"150","min_value":"15","max_value":"150"},
        ]
        let param = '{"route":"UpdateAlarmItem","ts":"'+getTs()+'","params":{"jsonData":"[]"}}'
        param = {
            route: "UpdateAlarmItem",
            ts: "UpdateAlarmItem",
            params: {
                jsonData: data
            },
        }
        socket.send(JSON.stringify(param))
    }
    function Login_AuthSuper() {
        let param = '{"route":"Login_AuthSuper","ts":"Login_AuthSuper","params":{"password":"123321"}}'
        console.log("发送参数: ", param)
        socket.send(param)
    }
    function TaskList() {
        let param = '{"route":"TaskList","ts":"TaskList","params":{"status":"2","page":1,"size":10}}'
        console.log("发送参数: ", param)
        socket.send(param)
    }
    function VTaskList() {
        let param = '{"route":"VTaskList","ts":"VTaskList","params":{"status":""}}'
        console.log("发送参数: ", param)
        socket.send(param)
    }
    function VTaskOptions2() {
        let param = {
            route: "VTaskOptions2",
            ts: "VTaskOptions2",
            params: {
                vid: "3",
            },
        }
        socket.send(JSON.stringify(param))
    }
    function VTaskOptions3() {
        let param = {
            route: "VTaskOptions3",
            ts: "VTaskOptions3",
            params: {},
        }
        socket.send(JSON.stringify(param))
    }
    function VTaskOptions() {
        let param = '{"route":"VTaskOptions","ts":"VTaskOptions","params":{"vid":"2"}}'
        console.log("发送参数: ", param)
        socket.send(param)
    }
    function TaskSave() {
        let data = [
            {"id":"21","itemid":"1","label":"TQ间距","sort":"1","value":"300m","text_alert":"0","img_alert":"1"},
            {"id":"22","itemid":"1","label":"水下航行体与对接装置末端间距","sort":"1","value":"298m","text_alert":"1","img_alert":"1"},
            {"id":"23","itemid":"1","label":"水下航行体与假体间距","sort":"1","value":"105m","text_alert":"1","img_alert":"0"},
            {"id":"24","itemid":"1","label":"水下航行体与对接装置末端间距","sort":"1","value":"210m","text_alert":"0","img_alert":"0"},
        ]
        let param = {
            route: "TaskSave",
            ts: "TaskSave",
            params: {
                jsonData: data,
                // task_id: msgEl.value,
                task_type : "1",
                vid1: "4",
                vid2: "12",
                vidname1: "背驮航行器",
                vidname2: "背驮航行器2",
                tname: "任务名称3",
                tcontent: "任务内容 BLAALBLA",
                option1: "母舰1",
            },
        }
        socket.send(JSON.stringify(param))
    }
    function TaskDetail() {
        let param = {
            route: "TaskDetail",
            ts: "TaskDetail",
            params: {
                task_id: "11",
            },
        }
        socket.send(JSON.stringify(param))
    }
    function TaskFinish() {
        let param = {
            route: "TaskFinish",
            ts: "TaskFinish",
            params: {
                task_id: msgEl.value,
            },
        }
        socket.send(JSON.stringify(param))
    }

    function ReadTaskData() {
        let param = {
            route: "ReadTaskData",
            ts: "bazhahei",
            params: {
                task_id: msgEl.value,
            },
        }
        socket.send(JSON.stringify(param))
    }

    function SetTimerInterval() {
        let param = {
            route: "SetTimerInterval",
            ts: "SetTimerInterval",
            params: {
                speed: speedEl.value,
            },
        }
        socket.send(JSON.stringify(param))
    }

    function ReadTaskDataPause() {
        let param = {
            route: "ReadTaskDataPause",
            ts: "ReadTaskDataPause",
            params: {
                speed: speedEl.value,
            },
        }
        socket.send(JSON.stringify(param))
    }

    function ReadTaskDataPlay() {
        let param = {
            route: "ReadTaskDataPlay",
            ts: "ReadTaskDataPlay",
            params: {
                speed: speedEl.value,
            },
        }
        socket.send(JSON.stringify(param))
    }

    function ClearTimerInterval() {
        let param = {
            route: "ClearTimerInterval",
            ts: "ClearTimerInterval",
            params: {},
        }
        socket.send(JSON.stringify(param))
    }

    function JumpToRecord() {
        let param = {
            route: "JumpToRecord",
            ts: "JumpToRecord",
            params: {
                percent: "10",
            },
        }
        socket.send(JSON.stringify(param))
    }

    function GetCurrentTaskId() {
        let param = {
            route: "GetCurrentTaskId",
            ts: "GetCurrentTaskId",
            params: {
                
            },
        }
        socket.send(JSON.stringify(param))
    }

    function ToPush() {
        let param = {
            route: "topushdata",
            ts: "topushdata",
            params: {
                
            },
        }
        socket.send(JSON.stringify(param))
    }

    function UpdateSimulationSettings() {
        let param = {
            route: "UpdateSimulationSettings",
            ts: "UpdateSimulationSettings",
            params: {
                speed: "1992",
            },
        }
        socket.send(JSON.stringify(param))
    }

    function UpdateForecastSettings() {
        let param = {
            route: "UpdateForecastSettings",
            ts: "UpdateForecastSettings",
            params: {
                api_type: "mother", // mother, uuv
                forecast_type: "Auto", // 可选 Auto, Grey, LSTM, Model
                window_length: 389, // int 类型
                window_forecast: 987, // int 类型
                model_name: "MotherPlantReal", // 可选数据不知道, 是个字符串就行
            },
        }
        socket.send(JSON.stringify(param))
    }

    function GetSimulationSettings() {
        let param = {
            route: "GetSimulationSettings",
            ts: "GetSimulationSettings",
            params: {
                speed: "2024",
            },
        }
        socket.send(JSON.stringify(param))
    }

</script>
</body>
</html>
